﻿using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace Game.UI
{
    [AddComponentMenu("Layout/UIHidable")]
    [RequireComponent(typeof(LayoutElement))]
    [RequireComponent(typeof(Toggle))]
    /// <summary>
    /// 利用Toggle实现的抽屉式Panel，点击时可变换大小。
    /// </summary>
    ///<remarks>如果是手工创建的，则需要确保registClick勾选，才会默认产生点击时的隐藏/显示事件</remarks>
	class UIPanelHidable : MonoBehaviour
    {
        public enum HideDirection { Vertical, Horizontal, NULL };

        //--子物体：需要隐藏的panel部分
        [SerializeField] RectTransform m_hidablePanel;

        [Tooltip("此UI扩展/隐藏目标对象时，本UI的布局扩展方向。")]
        [SerializeField] HideDirection m_hideDirection = HideDirection.Vertical;

        [Tooltip("此UI（非隐藏对象）默认的大小。保持为0则不更改")]
        //--默认（隐藏内容panel）时的大小
        [SerializeField] int m_defaultSize;

        [Tooltip("此UI（非隐藏对象）扩展出隐藏对象时的大小。保持为0则不更改")]
        //--展开Panel时的大小（忽略height）
        [SerializeField] int m_expandSize;

        [Tooltip("Regist click event of toggle when enable this component")]
        [SerializeField] bool m_registClick = false;

        [Tooltip("游戏开始时是否隐藏这个panel")]
        [SerializeField] bool m_hideWhenStart = true;
        /// <summary>
        /// Expand detail panel
        /// </summary>
        public void ShowPanel()
        {
            SwitchPanelTo(true);
        }

        /// <summary>
        /// Hide detail panel
        /// </summary>
        public void HidePanel()
        {
            SwitchPanelTo(false);
        }

        /// <summary>
        /// 在需要时对Toggle的valueChanged进行事件的注册，以实现勾选时的显示/隐藏效果
        /// </summary>
        private void Awake()
        {
            var toggleButton = GetComponent<Toggle>();
            if (m_registClick)
            {
                toggleButton.onValueChanged.AddListener((isOn) =>
                    {
                        if (isOn)
                            ShowPanel();
                        else
                            HidePanel();
                    });
                toggleButton.isOn = !m_hideWhenStart;
            }

            //hide the panel 
            if (m_hideWhenStart)
            {
                HidePanel();
            }
        }

        private void SwitchPanelTo(bool isOn)
        {

            if (isOn)
            {
                //允许不填写expandSize，默认开启Object即可
                if (m_expandSize > 0)
                {
                    //switch on
                    SetLayoutHeight(m_expandSize);
                }
            }
            else
            {
                //off
                //允许不填写defaultSize
                if (m_defaultSize > 0)
                {
                    SetLayoutHeight(m_defaultSize);
                }
            }

            m_hidablePanel.gameObject.SetActive(isOn);
        }

        private void SetLayoutHeight(int size)
        {
            LayoutElement layout = GetComponent<LayoutElement>();

            switch (m_hideDirection)
            {
                case HideDirection.Vertical:
                    layout.preferredHeight = size;
                    layout.minHeight = size;
                    break;

                case HideDirection.Horizontal:
                    layout.preferredWidth = size;
                    layout.minWidth = size;
                    break;

                case HideDirection.NULL:
                    Debug.Log("No layout will be changed.");
                    break;
            }
        }
    }
}
